package com.okyai.qa.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.okyai.common.annotation.Log;
import com.okyai.common.enums.BusinessType;
import com.okyai.qa.domain.NotesRecord;
import com.okyai.qa.service.INotesRecordService;
import com.okyai.common.core.controller.BaseController;
import com.okyai.common.core.domain.AjaxResult;
import com.okyai.common.utils.poi.ExcelUtil;
import com.okyai.common.core.page.TableDataInfo;

/**
 * 答题记录项目Controller
 * 
 * @author okyai
 * @date 2021-03-19
 */
@Controller
@RequestMapping("/qa/record")
public class NotesRecordController extends BaseController
{
    private String prefix = "qa/record";

    @Autowired
    private INotesRecordService notesRecordService;

    @RequiresPermissions("qa:record:view")
    @GetMapping()
    public String record()
    {
        return prefix + "/record";
    }

    /**
     * 查询答题记录项目列表
     */
    @RequiresPermissions("qa:record:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(NotesRecord notesRecord)
    {
        startPage();
        List<NotesRecord> list = notesRecordService.selectNotesRecordList(notesRecord);
        return getDataTable(list);
    }

    /**
     * 导出答题记录项目列表
     */
    @RequiresPermissions("qa:record:export")
    @Log(title = "答题记录项目", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(NotesRecord notesRecord)
    {
        List<NotesRecord> list = notesRecordService.selectNotesRecordList(notesRecord);
        ExcelUtil<NotesRecord> util = new ExcelUtil<NotesRecord>(NotesRecord.class);
        return util.exportExcel(list, "record");
    }

    /**
     * 新增答题记录项目
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存答题记录项目
     */
    @RequiresPermissions("qa:record:add")
    @Log(title = "答题记录项目", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(NotesRecord notesRecord)
    {
        return toAjax(notesRecordService.insertNotesRecord(notesRecord));
    }

    /**
     * 修改答题记录项目
     */
    @GetMapping("/edit/{answerId}")
    public String edit(@PathVariable("answerId") Long answerId, ModelMap mmap)
    {
        NotesRecord notesRecord = notesRecordService.selectNotesRecordById(answerId);
        mmap.put("notesRecord", notesRecord);
        return prefix + "/edit";
    }

    /**
     * 修改保存答题记录项目
     */
    @RequiresPermissions("qa:record:edit")
    @Log(title = "答题记录项目", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(NotesRecord notesRecord)
    {
        return toAjax(notesRecordService.updateNotesRecord(notesRecord));
    }

    /**
     * 删除答题记录项目
     */
    @RequiresPermissions("qa:record:remove")
    @Log(title = "答题记录项目", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(notesRecordService.deleteNotesRecordByIds(ids));
    }
}
